package edu.sctu.zhoudie.mouth201506.day20150615;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * Created by user08 on 2015/6/15.
 * 键盘输入数据的读取
 */
public class ReadData {
    public void readData() {
        int count;
        byte[] b = new byte[256];
        String str;
        BufferedInputStream bis = new BufferedInputStream(System.in);//输入缓冲区对象
        DataInputStream in = new DataInputStream(bis);//字节流输入对象
        try {
            if (in.markSupported()) {   //判断当前输入流是否支持mark和reset方法
                System.out.println("支持mark");
                System.out.println("输入字符串，按enter结束 ");
                in.mark(256);//在输入流的当前位置上设置标记，并保留256位
                count = in.read(b);
                System.out.println("读入字符数：" + count);
                str = new String(b, 0, count);
                System.out.println("输入字符串为：" + str);
                in.reset();  //重新回到标记处读取数据
                in.read(b, 0, 2);  //读前两个字符
                str = new String(b, 0, 2);
                System.out.println("字符串的前两个：" + str);
                in.reset();
                in.skip(count / 2);
                in.read(b, 0, count / 2);
                str = new String(b, 0, count / 2);
                System.out.println("字段的后半段：" + str);
            } else {
                System.out.println("不支持mrak!");
            }
            bis.close();
            in.close();
        } catch (IOException e) {
            System.out.println("发生IO错误！");
        }


    }
}
